Mastering SVG by Rob Larsen
Author:Rob Larsen
Language: eng
Format: epub, mobi
Tags: COM060080 - COMPUTERS / Web / General, COM070000 - COMPUTERS / User Interfaces, COM051260 - COMPUTERS / Programming Languages / JavaScript
Publisher: Packt Publishing
Published: 2018-09-21T05:23:53+00:00
Again, if you're familiar with DOM manipulations, then you'll notice some differences here. This is where the NS variable comes into play. Since this isn't pure HTML and is, in fact, an entirely different document definition, we need to supply that namespace in order to create the element properly. So, instead of document.createElement, we have to use document.createElementNS and we pass in a second argument referencing the SVG namespace via the NS variable.
Once the element is created, we set the relevant attributes using elem.setAttribute. For the rect we set x, y, width, and height. For the circle we set r, cx, and cy. For the text element we set x, y and then set the text content using elem.textContent, which is a new wrinkle if you're used to updating text and/or HTML nodes with innerHTML. As mentioned previously, there is no innerHTML of an SVG element.
Once the elem is defined with the baseline attributes, we insert it into the document using the appendChild method. Finally, we remove the "active" class from the target SVG element, which will prevent further elements being added accidentally:
function add(event) {
const classes = canvas.classList;
const NS = canvas.getAttribute('xmlns');
const point = canvas.createSVGPoint()
point.x = event.offsetX;
point.y = event.offsetY;
const svgCoords =
point.matrixTransform(canvas.getScreenCTM().inverse());
let elem;
if (classes.contains("active")) {
if (classes.contains("square")) {
elem = doc.createElementNS(NS, "rect");
elem.setAttribute("x", svgCoords.x);
elem.setAttribute("y", svgCoords.y);
elem.setAttribute("width", 50);
elem.setAttribute("height", 50);
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27095)
Hello! Python by Anthony Briggs(25950)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25286)
Kotlin in Action by Dmitry Jemerov(24396)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23591)
Dependency Injection in .NET by Mark Seemann(23313)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21945)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20849)
Grails in Action by Glen Smith Peter Ledbrook(19869)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17073)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16833)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14464)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12584)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11865)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10652)
Hit Refresh by Satya Nadella(9239)
The Kubernetes Operator Framework Book by Michael Dame(8588)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8446)
Robo-Advisor with Python by Aki Ranin(8391)